package com.boredom.Guts;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

import java.io.IOException;

/**
 * Methods providing useful file I/O operations
 *
 * This class consists of entirely static properties, and should never be
 * instanced.
 */
public class FileIO {
	/**
	 * Start of path, subject to change depending on choices about
	 * Android's directory structure.
	 */
	private static final String root = "/";

	/**
	 * Determine the pathname for the directory a block of metadata
	 *
	 * @param metaData the metadata from the start of the file
	 * @return the pathname of the directory to write the file to
	 */
	private static String findPathname (MetaData metaData) {
		return root + metaData.getInterestGroup() + "/" +
			metaData.getType() + "/" + metaData.getSource() + "/";
	}

	/**
	 * Write a string to a file as dictated by its metadata.
	 *
	 * Attempts to write a string to a file. The metadata dictates where
	 * the file will be situated in the directory tree, is used to
	 * determine if the file already exists, and whether it should be
	 * overwritten.
	 *
	 * @param metaData The file's metaData
	 * @param contents The actual contents to be in the file
	 * @return true if the file was written, false if a newer version exists
	 */
	public static boolean write (MetaData metaData, String contents) {
		String pathname;

		pathname = findPathname (metaData);

		return true;
	}

	/**
	 * Write a stream to a file as dictated by its metadata.
	 *
	 * Attempts to write a stream to a file. The metadata dictates where
	 * the file will be situated in the directory tree, is used to
	 * determine if the file already exists, and whether it should be
	 * overwritten.
	 *
	 * @param metaData The file's metaData
	 * @param contents The stream representing the file, pointing to the
	 *  section just after the metadata
	 * @return true if the file was written, false if a newer version exists
	 */
	public static boolean write (MetaData metaData, InputStream contents) {
		String pathname;

		pathname = findPathname (metaData);

		return true;
	}

	/**
	 * Write a string to a file, parsing metadata
	 *
	 * Attempts to write a string to a file. The metadata dictates where
	 * the file will be situated in the directory tree, is used to
	 * determine if the file already exists, and whether it should be
	 * overwritten.
	 *
	 * @param string The string to be written to the file
	 * @return true if the file was written, false if a newer version exists
	 */
	public boolean save (String string) throws IOException {
		InputStream is
			= new ByteArrayInputStream (string.getBytes());
		MetaData metaData;

		metaData = new MetaData (is);

		return write (metaData, is);
	}

	/**
	 * Write a stream to a file, parsing metadata
	 *
	 * Attempts to write a stream to a file. The metadata dictates where
	 * the file will be situated in the directory tree, is used to
	 * determine if the file already exists, and whether it should be
	 * overwritten.
	 *
	 * @param stream The stream to be written to the file
	 * @return true if the file was written, false if a newer version exists
	 */
	public boolean save (InputStream stream) throws IOException {
		String pathname;
		MetaData metaData;

		metaData = new MetaData (stream);

		return write (metaData, stream);
	}
}
